﻿class DragScreenModule {

	private var instance:MovieClip;
	private var targetClip:MovieClip;
	private var dragClip:MovieClip;
	private var dragBackClip:MovieClip;
	private var dragHiddenBnClip:MovieClip;
	public var dragBnBgLength:Number;
	private var wheelDoIt:Boolean;

	public var dragClipInitY:Number;
	public var targetClipInitY:Number;
	public var targetClipLimitHeight:Number;

	//생성자 함수 초기화
	public function DragScreenModule(_instance:MovieClip, _targetClip:MovieClip, _dragClip:MovieClip, _dragBackClip:MovieClip, _dragHiddenBnClip:MovieClip, _dragBnBgLength:Number, _dragClipInitY:Number, _targetClipInitY:Number, _targetClipLimitHeight:Number, _wheelDoIt:Boolean) {
		this.instance = _instance;
		this.targetClip = _targetClip;
		this.dragClip = _dragClip;
		this.dragBackClip = _dragBackClip;
		this.dragHiddenBnClip = _dragHiddenBnClip;
		this.dragBnBgLength = _dragBnBgLength;
		this.wheelDoIt = _wheelDoIt;

		this.dragClipInitY = _dragClipInitY;
		this.targetClipInitY = _targetClipInitY;
		this.targetClipLimitHeight = _targetClipLimitHeight;

		var dragBarControl:MovieClip = this.instance.createEmptyMovieClip("dragBarControl", 100);
		var dragBarMotion:MovieClip = this.instance.createEmptyMovieClip("dragBarMotion", 110);
		var screenControl:MovieClip = this.instance.createEmptyMovieClip("screenControl", 120);

		var dragBn:DragScreenButton = new DragScreenButton(this.instance, this.dragClip, this.dragHiddenBnClip, this.dragBnBgLength);

		if (this.wheelDoIt == true) {
			var wheelMouse:WheelModule = new WheelModule(this.instance, this.dragClip);
		}
		//       
		dragClip._y = dragClipInitY;
		//

		this.screenCheckControl();

	}
	
	public function screenCheckControl():Void {

		this.barVisibleCheck();

	}

	public function dragControl():Void {

		var targetClip:MovieClip = this.targetClip;
		var dragClip:MovieClip = this.dragClip;
		var instance:MovieClip = this.instance;
		var dragBnBgLength:Number = this.dragBnBgLength;
		
		var dragClipInitY:Number = this.dragClipInitY;
		var targetClipInitY:Number = this.targetClipInitY;
		var targetClipLimitHeight:Number = this.targetClipLimitHeight;		

		this.instance.dragBarControl.onEnterFrame = function() {
			var distaceDiff:Number = (Math.abs(targetClipLimitHeight-targetClip._height)/Math.abs(dragBnBgLength-dragClip._height))*(dragClipInitY-dragClip._y);
			targetClip.targetY = targetClipInitY+distaceDiff;
			targetClip._y += 0.2*(targetClip.targetY-targetClip._y);

			if (instance.wheelCheck == false) {
				if (Math.abs(targetClip._y-targetClip.targetY)<0.5) {
					targetClip._y = targetClip.targetY;
				}
			} else if (instance.wheelCheck == true) {
				if (Math.abs(targetClip._y-targetClip.targetY)<0.5) {
					targetClip._y = targetClip.targetY;
					delete instance.dragBarControl.onEnterFrame;
				}
			}
		};
	}

	public function dragMotionControl():Void {

		var targetClip:MovieClip = this.targetClip;
		var dragClip:MovieClip = this.dragClip;
		var instance:MovieClip = this.instance;
		var dragBnBgLength:Number = this.dragBnBgLength;
		
		var dragClipInitY:Number = this.dragClipInitY;




		this.instance.dragBarMotion.onEnterFrame = function() {
			Smooth.smoothMove(dragClip,0.25,dragClip._x,dragClip.targetYpos);
			if (dragClip.targetYpos>=dragClipInitY+dragBnBgLength-dragClip._height && dragClip._y>=dragClipInitY+dragBnBgLength-dragClip._height) {
				dragClip._y = dragClipInitY+dragBnBgLength-dragClip._height;
			}
			if (dragClip.targetYpos<=dragClipInitY && dragClip._y<=dragClipInitY) {
				dragClip._y = dragClipInitY;
			}
			if (Math.abs(dragClip._y-dragClip.targetYpos)<0.5) {
				dragClip._y = dragClip.targetYpos;
				delete this.onEnterFrame;
			}
		};
	}

	public function barVisibleCheck():Void {

		var targetClip:MovieClip = this.targetClip;
		var dragClip:MovieClip = this.dragClip;
		var instance:MovieClip = this.instance;
		var dragBackClip:MovieClip = this.dragBackClip;
		var dragHiddenBnClip:MovieClip = this.dragHiddenBnClip;		
		var targetClipLimitHeight:Number = this.targetClipLimitHeight;				

		if (targetClip._height>targetClipLimitHeight) {
			dragClip._visible = true;
			dragBackClip._visible = true;
			dragHiddenBnClip._visible = true;	
			instance.wheelCheck = true;


		} else if (targetClip._height<=targetClipLimitHeight) {
			dragClip._visible = false;
			dragBackClip._visible = false;
			dragHiddenBnClip._visible = false;
			instance.wheelCheck = false;
		}
	}
}